import struct
from uuid import UUID
from string import atoi

class FieldData:
    """models field data"""

    def __init__(self,field,value=None):
        """initialize field data"""
        self._field = field
        self._value=value

    def setValue(self,value):
        self._value = value

    def getValue(self):
        return self._value 

    value = property(getValue,setValue)

    def toBytes(self):
        """convert this to bytes"""
        return self._field.toBytes(self.value)

class BlockData:
    """models a data block for a message"""

    def __init__(self,block):
        """initialize block data"""
        self._block = block
        self._fieldlist = []

        for field in block:
            # TODO: handle variable and mutiple fields
            fieldData = FieldData(field)
            setattr(self, field.name, fieldData)
            self._fieldlist.append(fieldData)

    def toBytes(self):
        """return the byte representation"""
        d=""
        for fieldData in self._fieldlist:
            d+=fieldData.toBytes()
        return d 

class Message:
    """a message object"""

    def __init__(self,tmpl):
        """initialize a message according to a template"""
        self._tmpl = tmpl
        self._blocklist = []

        # initialize all blocks
        for block in tmpl:
            # TODO: handle variable and mutiple blocks
            blkData = BlockData(block)
            setattr(self,block.name,blkData)
            self._blocklist.append(blkData)

    def toBytes(self):
        """return the byte representation"""
        d=""
        for blkdata in self._blocklist:
            d+=blkdata.toBytes()
        return d 
        

